/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.model.manteniment.auxiliars;
import comandaXM.classes.manteniment.auxiliar.Pais;
import comandaXM.classes.manteniment.auxiliar.Provincia;
import comandaXM.classes.manteniment.proveidor.*;
import comandaXM.excepcions.ExceptionHibernate;
import comandaXM.utils.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

/**
 *
 * @author xmarti
 */
//TODO: CAL IMPLEMENTAR ELS TIPUS D¡'IVA, I REVISAR LA CERCA DE PROVINCIES
public class ProveidorModel extends HibernateUtil {
     public Proveidor getProveidor(int id) throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Proveidor resultList = new Proveidor(); 
        try
        {            
            resultList= (Proveidor) session.createQuery("FROM Proveidor p where p.id="+id ).uniqueResult();
            
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    
     public List<Proveidor> getListProveidorsByNomActius(String textToSearch) throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        
        List<Proveidor> resultList = new ArrayList<Proveidor>();
        try
        {                    
            resultList= session.createQuery("FROM Proveidor p Where"
                    + " p.nomFiscal LIKE '%"+ textToSearch  +"%' order by p.nomFiscal" ).list();
        }
        catch (HibernateException he)
        {     
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
            
        }
        return resultList;
    }
     public List<Proveidor> getListProveidors(String codi, String cif, String nomFiscal, String nomComercial, String direccio, String poblacio, String codiPostal,Provincia provincia, Pais pais ) throws ExceptionHibernate{
        String stringSql = ""; 
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
       
        if (!codi.equals("")) {
            stringSql=  " p.id = " + codi  +"";
        }else{            
            if (!cif.equals("")) stringSql=  " p.cif LIKE '"+ cif  +"%' ";
            if(!nomFiscal.equals("")){
                if(stringSql.equals("")){
                    stringSql=  " p.nomFiscal LIKE '"+ nomFiscal  +"%' ";                               
                }else{
                    stringSql=stringSql +  " AND p.nomFiscal LIKE '"+ nomFiscal  +"%' "; 
                }
            }
            if(!nomComercial.equals("")){
                if(stringSql.equals("")){
                    stringSql=  " p.nomComercial LIKE '"+ nomComercial  +"%' ";                               
                }else{
                    stringSql=stringSql +  " AND p.nomComercial LIKE '"+ nomComercial  +"%' "; 
                }
            }
            if(!direccio.equals("")){
                if(stringSql.equals("")){
                    stringSql=  " p.adressaFiscal LIKE '"+ direccio  +"%' ";                               
                }else{
                    stringSql=stringSql +  " AND p.adressaFiscal LIKE '"+ direccio  +"%' "; 
                }
            }
            if(!poblacio.equals("")){
                if(stringSql.equals("")){
                    stringSql=  " p.poblacio LIKE '"+ poblacio  +"%' ";                               
                }else{
                    stringSql=stringSql +  " AND p.poblacio LIKE '"+ poblacio  +"%' "; 
                }
            }
            if(!codiPostal.equals("")){
                if(stringSql.equals("")){
                    stringSql=  " p.codiPostal LIKE '"+ codiPostal  +"%' ";                               
                }else{
                    stringSql=stringSql +  " AND p.codiPostal LIKE '"+ codiPostal  +"%' "; 
                }
            }
            if(provincia instanceof Provincia ){
                if(stringSql.equals("")){
                    stringSql=  " p.provincia.id = "+ provincia.getId();                               
                }else{
                    stringSql=stringSql +  " AND p.provincia.id = "+  provincia.getId(); 
                }
            }
            if(pais instanceof Pais ){
                if(stringSql.equals("")){
                    stringSql=  " p.pais.id = "+ pais.getId();                               
                }else{
                    stringSql=stringSql +  " AND p.pais.id = "+  pais.getId(); 
                }
            }            
        }
        
        List<Proveidor> resultList = new ArrayList<Proveidor>();
        try
        {                  
//            if (stringSql.length()==0){
//                //resultList= session.createQuery("FROM Proveidor p  order by p.nomFiscal" ).list();
//                resultList= session.createQuery("FROM Proveidor p  order by p.nomFiscal" ).list();
//            }else{    
//                resultList= session.createQuery("FROM Proveidor p Where "
//                    + stringSql + " order by p.nomFiscal" ).list();
//            }
            if (stringSql.length()==0){
                //resultList= session.createQuery("FROM Proveidor p  order by p.nomFiscal" ).list();
                resultList= session.createQuery("select distinct p FROM Proveidor p left join fetch p.centres c left join fetch c.contactes left join fetch p.notificacions  order by p.nomFiscal" ).list();
            }else{
                resultList= session.createQuery("select distinct p FROM Proveidor p left join fetch p.centres c left join fetch c.contactes left join  fetch p.notificacions Where "
                    + stringSql + " order by p.nomFiscal" ).list();
            }
        }
        catch (HibernateException he)
        {     
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
            
        }
        return resultList;
    }
    public int add(Proveidor object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                                        
                session.save(object);                                                                            
                state= object.getId();                
                session.getTransaction().commit();
        }catch (Exception e){			
                session.getTransaction().rollback();		
                if (e.getCause().toString().contains("name_UNIQUE") ){
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT_NAME_UNIQUE);
                }else{
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT);			
                }  
                
        }		
        return state;
	}
    public int add(CentreProveidor object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                                        
                session.save(object);                                                                                
                state= object.getId();                
                session.getTransaction().commit();
        }catch (Exception e){			
                session.getTransaction().rollback();		                
                throw new ExceptionHibernate(ExceptionHibernate.getMessage(e.getMessage()));                
        }		
        return state;
	}
    public int add(Contacte object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                                        
                session.save(object);                                                                                
                state= object.getId();                
                session.getTransaction().commit();
        }catch (Exception e){			
                session.getTransaction().rollback();		                
                throw new ExceptionHibernate(ExceptionHibernate.getMessage(e.getMessage()));                
        }		
        return state;
	}
    public int add(Notificacio object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                                        
                session.save(object);                                                                                
                state= object.getId();                
                session.getTransaction().commit();
        }catch (Exception e){			
                session.getTransaction().rollback();		                
                throw new ExceptionHibernate(ExceptionHibernate.getMessage(e.getMessage()));                
        }		
        return state;
	}
}
